/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Connection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author My PC
 */
public class Connect {
    public static java.sql.Connection myCon=null;
    public static Statement stmt = null;
    public static ResultSet rs=null;
    public static Connection connection;
    public Connection getConnectToSQL()
    {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            connection = DriverManager.getConnection("jdbc:sqlserver://MYPC\\SQLEXPRESS:1433;databaseName=DIENTHOAI","sa","sa");
        } catch (SQLException ex) {
            Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex);
        }
        return connection;
    }
    public static void ExercuteSql(String sql)
    {
        try{
//           if(myCon==null)
//           {
//               JOptionPane.showMessageDialog(null,"Lỗi Startment  ");
//           }
            stmt=connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
        }catch(SQLException ex){
            JOptionPane.showMessageDialog(null,"Lỗi Startment  "+ex.getMessage());
        }
        try {
            rs=stmt.executeQuery(sql);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null,"Lỗi câu lệnh truy vấn "
                    +ex.getMessage());            
        }           
    }
    
    public static void UpdateSQL(String sql)
    {
        try{
            stmt=connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
        }catch(SQLException ex){
           JOptionPane.showMessageDialog(null,"Lỗi Startment"+ex.getMessage());
        }
        try {
            stmt.executeUpdate(sql);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null,"Lỗi câu lệnh truy vấn "
                    +ex.getMessage());            
        }          
    }
    public void Close()
    {
        if(connection!=null) {
            try {
                if(!connection.isClosed()) {
                    connection.close();
                }
        } catch (SQLException ex) {
            Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex);
        }
        }
        connection=null;
    }
     
    
}
